/* =========================================================
 * ====                   WARNING                        ===
 * =========================================================
 * The code in this tab has been generated from the GUI form
 * designer and care should be taken when editing this file.
 * Only add/edit code inside the event handlers i.e. only
 * use lines between the matching comment tags. e.g.

 void myBtnEvents(GButton button) { //_CODE_:button1:12356:
     // It is safe to enter your event code here  
 } //_CODE_:button1:12356:
 
 * Do not rename this tab!
 * =========================================================
 */

public void imgTogButton1_click1(GImageToggleButton source, GEvent event) { //_CODE_:imgTogButton1:666382:

  if (!gameStarted) {
    if (imgTogButton1.getState() == 1)
    { 
      DIMENSION = 3;
      println("4");
    } else
    {
      DIMENSION = 4;
      println("8");
    }
    TILESIZE=CAMERA_SCREEN_SIZE/DIMENSION;
  }
} //_CODE_:imgTogButton1:666382:

public void imgButton1_click1(GImageButton source, GEvent event) { //_CODE_:imgButton1:901677:
  println("imgButton1 - GImageButton >> GEvent." + event + " @ " + millis());
  
    if (!restart)
  {
    savedTime=millis();
    scoreTime = -10000;
    gameStarted = true;
    restart = true;
  }
} //_CODE_:imgButton1:901677:

public void imgButton2_click1(GImageButton source, GEvent event) { //_CODE_:imgButton2:855565:
  println("imgButton2 - GImageButton >> GEvent." + event + " @ " + millis());
  
  gameStarted = false;
  takeSnapshot = false;
  rankingShown = false;
  WINNER = false;
  isSaved = false;
  restart = false;
  
} //_CODE_:imgButton2:855565:

synchronized public void win_draw1(GWinApplet appc, GWinData data) { //_CODE_:rankingWindow:498765:
  appc.background(230);
} //_CODE_:rankingWindow:498765:

public void button3_click1(GButton source, GEvent event) { //_CODE_:button3:765056:
  println("button3 - GButton >> GEvent." + event + " @ " + millis());

  XML[] children = xml.getChildren("player");
  for (int i = 0; i < children.length; i++)
  {
    xml.removeChild(children[i]);
    if (children[i].getInt("score")>=(scoreTime*(-1)) && !isSaved)
    {
      children[i].setInt("score", scoreTime*(-1));
      children[i].setString("name", textfield1.getText());

      isSaved=!isSaved;
    }
    xml.addChild( children[i]);
  }
  label3.setText(" ");
  for (int i = 0; i < children.length; i++)
  {
    label3.setText(label3.getText()+"\n"+children[i].getString("id")+":  "+ children[i].getString("name")+"   "+ children[i].getString("score") );
  }

  saveXML(xml, "/data/ranking.xml");
  isSaved = false;
} //_CODE_:button3:765056:

public void textfield1_change1(GTextField source, GEvent event) { //_CODE_:textfield1:885626:
  println("textfield1 - GTextField >> GEvent." + event + " @ " + millis());
} //_CODE_:textfield1:885626:



// Create all the GUI controls. 
// autogenerated do not edit
public void createGUI(){
  G4P.messagesEnabled(false);
  G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
  G4P.setCursor(ARROW);
  if(frame != null)
    frame.setTitle("Sketch Window");
  label1 = new GLabel(this, 10, 485, 347, 150);
  label1.setTextAlign(GAlign.LEFT, GAlign.TOP);
  label1.setText("My label");
  label1.setLocalColorScheme(GCScheme.PURPLE_SCHEME);
  label1.setOpaque(true);
  label2 = new GLabel(this, 375, 545, 100, 35);
  label2.setTextAlign(GAlign.LEFT, GAlign.TOP);
  label2.setText("Your time:");
  label2.setLocalColorScheme(GCScheme.PURPLE_SCHEME);
  label2.setOpaque(true);
  imgTogButton1 = new GImageToggleButton(this, 500, 500);
  imgTogButton1.addEventHandler(this, "imgTogButton1_click1");
  label4 = new GLabel(this, 490, 490, 60, 80);
  label4.setTextAlign(GAlign.CENTER, GAlign.BOTTOM);
  label4.setText("difficulty");
  label4.setTextBold();
  label4.setOpaque(false);
  label5 = new GLabel(this, 525, 491, 80, 20);
  label5.setText("easy");
  label5.setOpaque(false);
  label6 = new GLabel(this, 530, 530, 80, 20);
  label6.setText("normal");
  label6.setOpaque(false);
  imgButton1 = new GImageButton(this, 375, 495, 100, 35, new String[] { "begin1.png", "begin1.png", "begin1.png" } );
  imgButton1.addEventHandler(this, "imgButton1_click1");
  imgButton2 = new GImageButton(this, 375, 595, 100, 35, new String[] { "restart1.png", "restart1.png", "restart1.png" } );
  imgButton2.addEventHandler(this, "imgButton2_click1");
  rankingWindow = new GWindow(this, "Ranking", 0, 0, 240, 480, false, JAVA2D);
  rankingWindow.setActionOnClose(G4P.CLOSE_WINDOW);
  rankingWindow.addDrawHandler(this, "win_draw1");
  label3 = new GLabel(rankingWindow.papplet, 10, 9, 220, 390);
  label3.setText("scores");
  label3.setLocalColorScheme(GCScheme.PURPLE_SCHEME);
  label3.setOpaque(false);
  button3 = new GButton(rankingWindow.papplet, 130, 430, 80, 30);
  button3.setText("save");
  button3.setTextBold();
  button3.setLocalColorScheme(GCScheme.PURPLE_SCHEME);
  button3.addEventHandler(this, "button3_click1");
  textfield1 = new GTextField(rankingWindow.papplet, 7, 430, 110, 30, G4P.SCROLLBARS_NONE);
  textfield1.setText("name");
  textfield1.setLocalColorScheme(GCScheme.PURPLE_SCHEME);
  textfield1.setOpaque(true);
  textfield1.addEventHandler(this, "textfield1_change1");
}

// Variable declarations 
// autogenerated do not edit
GLabel label1; 
GLabel label2; 
GImageToggleButton imgTogButton1; 
GLabel label4; 
GLabel label5; 
GLabel label6; 
GImageButton imgButton1; 
GImageButton imgButton2; 
GWindow rankingWindow;
GLabel label3; 
GButton button3; 
GTextField textfield1; 

